﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain;
using Ue.Qcdz.Domain.Roles;

namespace Ue.Qcdz.UI.Controls.Membership
{
    public class AjaxLogin : SkinnedControl
    {
        #region Overrides of SkinnedControl

        private Panel spanLogin;
        private Panel spanUserinfo;
        private Panel spanAdmin;
        private Label lbUsername;
        private string strFileName = "~/Admin/Themes/Skins/Membership/UserLogin.ascx";

        protected override string GetSkinFileName()
        {
            return strFileName;
        }

        protected override void InitializeSkin(Control skin)
        {
            spanLogin = skin.FindControl<Panel>("spanLogin");
            spanUserinfo = skin.FindControl<Panel>("spanUserinfo");
            lbUsername = skin.FindControl<Label>("lbUsername");
            spanAdmin = skin.FindControl<Panel>("spanAdmin");
           
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                lbUsername.Text = HttpContext.Current.User.Identity.Name;
                spanUserinfo.Style["Display"] = "block";
                spanLogin.Style["Display"] = "none";

                Guid roleIdAdmion = new Guid("D58B0844-FB50-41AA-9CE2-38C50892E5D0");
                Guid roleIdNewsAdmin = new Guid("F2CD22CC-E217-4F50-87E7-97DBA9197CA8");
                IRoleRepository repository =
                Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IRoleRepository, Role>();
                Role roleAdmin = repository.FindBy(roleIdAdmion);
                Role roleNewsAdmin = repository.FindBy(roleIdNewsAdmin);
                if (SiteContext.GetSiteContext().User.Roles.Contains(roleAdmin) || SiteContext.GetSiteContext().User.Roles.Contains(roleNewsAdmin))
                {
                    spanAdmin.Style["Display"] = "block";
                }
                else
                {
                    spanAdmin.Style["Display"] = "none";
                }
            }
            else
            {
                lbUsername.Text = "";
                spanAdmin.Style["Display"] = "none";
                spanUserinfo.Style["Display"] = "none";
                spanLogin.Style["Display"] = "block";
                
            }
        }

        #endregion
    }
}
